/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox.particles

import com.almasb.fxgl.app.*
import com.almasb.fxgl.core.math.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.entity.*
import com.almasb.fxgl.input.*
import com.almasb.fxgl.particle.*
import javafx.geometry.*
import javafx.scene.effect.*
import javafx.scene.input.*
import javafx.scene.paint.*
import javafx.util.*

/**
 * Using particles with source images and colorization.
 *
 * @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)
 */
class SmokeSample : GameApplication() {
	private lateinit var emitter: ParticleEmitter
	private lateinit var e: ParticleEmitter
	private lateinit var entity: Entity
	
	override fun initSettings(settings: GameSettings) {}
	
	override fun initGame() {
		getGameScene().setBackgroundColor(Color.BLACK)
		
		e = ParticleEmitters.newSmokeEmitter()
		e.blendMode = BlendMode.SRC_OVER
		e.setSize(15.0, 30.0)
		e.numParticles = 10
		e.emissionRate = 0.25
		e.startColor = Color.color(0.6, 0.55, 0.5, 0.47)
		e.endColor = Color.BLACK
		e.setExpireFunction { Duration.seconds(16.0) }
		e.setVelocityFunction { Point2D(FXGLMath.randomDouble() - 0.5, 0.0) }
		
		//        Entities.builder()
		//                .at(getWidth() / 2, getHeight() - 100)
		//                .with(new ParticleComponent(e), new RandomMoveControl(2))
		//                .buildAndAttach(getGameWorld());
		
		
		emitter = ParticleEmitters.newFireEmitter()
		//        emitter.setSize(5, 15);
		//        emitter.setVelocityFunction(i -> new Point2D(FXGLMath.random() - 0.5, -FXGLMath.random() * 3));
		//        emitter.setAccelerationFunction(() -> new Point2D(0, 0.05));
		//        emitter.setExpireFunction(i -> Duration.seconds(3));
		//        emitter.setScaleFunction(i -> new Point2D(FXGLMath.random(0, 0.01), FXGLMath.random(-0.05, 0.05)));
		//        emitter.setStartColor(Color.YELLOW);
		//        emitter.setEndColor(Color.RED);
		//        emitter.setBlendMode(BlendMode.SRC_OVER);
		//        emitter.setSourceImage(texture("particleTexture2.png").toColor(Color.rgb(230, 75, 40)).getImage());
		
		entity = entityBuilder()
			.with(ParticleComponent(emitter))
			.buildAndAttach()
		
		//        entity = Entities.builder()
		//                .at(getWidth() / 2, getHeight() / 2)
		//                .with(new ParticleComponent(emitter))
		//                .buildAndAttach(getGameWorld());
		//
		//
		//
		//        Entities.builder()
		//                .at(250, 250)
		//                .viewFromNode(new Rectangle(40, 40, Color.BLUE))
		//                .with(new CircularMovementComponent(10, 25))
		//                .buildAndAttach(getGameWorld());
	}
	
	override fun initInput() {
		getInput().addAction(object : UserAction("Change Color") {
			override fun onActionBegin() {
				val randomColor = Color.color(FXGLMath.randomDouble(), FXGLMath.randomDouble(), FXGLMath.randomDouble())
				emitter.blendMode = BlendMode.SRC_OVER
				e.startColor = randomColor
				e.endColor = Color.color(FXGLMath.randomDouble(), FXGLMath.randomDouble(), FXGLMath.randomDouble())
			}
		}, MouseButton.PRIMARY)
	}
	
	override fun onUpdate(tpf: Double) {
		entity.x = getInput().mouseXWorld - 25
		entity.y = getInput().mouseYWorld - 25
	}
}

fun main(args: Array<String>) {
	GameApplication.launch(SmokeSample::class.java, args)
}

